###
TODO: add support for browsers that dont support localStorage by using cookies and json instead
###
class tree.persistence.Storage extends tree.BaseObject

    constructor: (@app)->
        super
        if (localStorage.getItem @app) is null
            localStorage.setItem @app, tree.io.formats.json.dump {}

    save: (key, val) ->
        value = (tree.io.formats.json.load (localStorage.getItem @app))
        value[key] = val
        localStorage.setItem @app, tree.io.formats.json.dump value
        @emit 'changed', {type: 'saved', key: key, val: val}
    
    get_all: () ->
        tree.io.formats.json.load localStorage.getItem @app
        
    clear: () ->
        localStorage.setItem @app, tree.io.formats.json.dump {}
    
    get: (key) ->
        value = tree.io.formats.json.load localStorage.getItem @app
        if (value[key]) == null
            return ''
        value[key]
    
    remove: (key) ->
        localStorage.remove key
        @emit 'changed', {type: 'removed', key: key}
